<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\PetComment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FindPetController extends Controller
{

    /**
     * 获取寻宠列表
     * @return \Illuminate\Support\Collection|string
     */
    public function getFindPetList(Request $request)
    {
        $pageSize = $request->input('pageSize');
        $list = DB::table('pet_find_pet')
            ->join('pet_user', 'pet_find_pet.uid', '=', 'pet_user.user_id')
            ->select('pet_find_pet.*', 'pet_user.nickname')
            ->paginate($pageSize);
        if ($list) {
            foreach ($list as $key => $value) {
                $list[$key]->image = config('app.url') . '/uploads/' . $value->image;
            }
            return $list;
        }
        return '暂无数据';
    }


    /**
     * 获取寻宠消息详情
     * @param Request $request
     * @return \Illuminate\Support\Collection|string
     */
    public function getFindPetDetail(Request $request){
        $find_id = $request->input('find_id');
        $list = DB::table('pet_find_pet')
            ->join('pet_user', 'pet_find_pet.uid', '=', 'pet_user.user_id')
            ->select('pet_find_pet.*', 'pet_user.nickname')
            ->where('find_id','=',$find_id)
            ->get();
        if ($list) {
            foreach ($list as $key => $value) {
                $list[$key]->image = config('app.url') . '/uploads/' . $value->image;
            }
            return $list;
        }
        return '暂无数据';
    }


    /**
     * 增加寻宠消息
     * @param Request $request
     * @return bool
     */
    public function addFindPet(Request $request){
      $res = DB::table('pet_find_pet')->insert(
            [
                'title' => $request->input('title'),
                'image' => $request->input('image'),
                'content' => $request->input('content'),
                'uid' => $request->input('user_id'),
                'province_id' => $request->input('province_id'),
                'city_id' => $request->input('city_id'),
                'area' => $request->input('area'),
                'phone' => $request->input('phone'),
                'latitude' => $request->input('latitude'),
                'longitude' => $request->input('longitude'),
                'created_at' => date("Y-m-d H:i:s")
        ]
        );
      return $res;
    }


    /**
     * 获取评论
     * @param Request $request
     * @return \Illuminate\Support\Collection
     */
    public function getComment(Request $request){
        $find_id = $request->input('find_id');
        $commentList = DB::table('pet_comment')
            ->where('find_id',$find_id)
            ->join('pet_user', 'pet_comment.user_id', '=', 'pet_user.user_id')
            ->select('pet_comment.*', 'pet_user.nickname', 'pet_user.headimgurl')
            ->get();
        return $commentList;
    }

    /**
     * 添加评论
     * @param Request $request
     * @return array|bool
     */
    public function addComment(Request $request){
        $user_id = $request->input('user_id');
        $check_user = DB::table('pet_user')->where('user_id',$user_id)->get();
        if (!$check_user) return [404,'用户错误'];
        $res = DB::table('pet_comment')->insert(
            [
                'user_id' => $request->input('user_id'),
                'find_id' => $request->input('find_id'),
                'content' => $request->input('content'),
                'parent_id' => $request->input('parent_id'),
                'is_read' => 0,
                'created_at' => date("Y-m-d H:i:s")
            ]
        );
        return $res;
    }

}
